package jst.householdmanager.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import jst.householdmanager.database.table.Items;

public class ItemsDataSource {

	public static final String TABLE_NAME = "Items";
	public static final String COLUMN_ID = "item_ID";
	public static final String COLUMN_NAME = "item_Name";
	public static final String COLUMN_DESC = "item_Desc";

	public static final String[] ALL_COLUMNS = { COLUMN_ID, COLUMN_NAME, COLUMN_DESC };

	public static final int[] COLUMN_DATATYPE = { 0, 1, 1 };

	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_ID + " INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL, "
			+ COLUMN_NAME + " VARCHAR(75) NOT NULL UNIQUE, " + COLUMN_DESC + " TEXT);";

	public static void save(Items dataRecord) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME, dataRecord.getName());
		values.put(COLUMN_DESC, dataRecord.getDescription());

		if (dataRecord.getId() == -1) {
			long insertID = SQLiteDataSource.getDatabase().insert(TABLE_NAME, null, values);
			dataRecord.setId((int) insertID);
		} else {
			SQLiteDataSource.getDatabase().update(TABLE_NAME, values, COLUMN_ID + " = ?", new String[] { Integer.toString(dataRecord.getId()) });
		}
	}

	public static void delete(String selection, String[] selectionArgs) {
		SQLiteDataSource.getDatabase().delete(TABLE_NAME, selection, selectionArgs);
	}

	public static Items read(String selection, String[] selectionArgs) {
		Items dataRecord = null;
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, null, null, null);
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			dataRecord = cursorTo(cursor);
		}
		cursor.close();

		return dataRecord;
	}

	public static List<Items> readAll(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
		List<Items> dataRecords = new ArrayList<Items>();
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, groupBy, having, orderBy);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			dataRecords.add(cursorTo(cursor));
			cursor.moveToNext();
		}
		cursor.close();

		return dataRecords;
	}

	public static void delete(Items item) {
		delete(COLUMN_ID + " = ?", new String[] { Integer.toString(item.getId()) });
	}

	public static Items read(int itemId) {
		return read(COLUMN_ID + " = ?", new String[] { Integer.toString(itemId) });
	}

	public static List<Items> readAll(String selection, String[] selectionArgs) {
		return readAll(selection, selectionArgs, null, null, null);
	}

	public static List<Items> readAll() {
		return readAll(null, null, null, null, null);
	}

	private static Items cursorTo(Cursor cursor) {
		return new Items(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
	}

}
